home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / info-service / gopher / Unix / gopher1.12 / object / GDgopherdir.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-30  |  4.7 KB  |  267 lines

  1. /********************************************************************
  2.  * $Author: lindner $
  3.  * $Revision: 1.1 $
  4.  * $Date: 1992/12/10 23:27:52 $
  5.  * $Source: /home/mudhoney/GopherSrc/release1.11/object/RCS/GDgopherdir.c,v $
  6.  * $Status: $
  7.  *
  8.  * Paul Lindner, University of Minnesota CIS.
  9.  *
  10.  * Copyright 1991, 1992 by the Regents of the University of Minnesota
  11.  * see the file "Copyright" in the distribution for conditions of use.
  12.  *********************************************************************
  13.  * MODULE: GDgopherdir.c
  14.  * Implement gopher directory routines
  15.  *********************************************************************
  16.  * Revision History:
  17.  * $Log: GDgopherdir.c,v $
  18.  * Revision 1.1  1992/12/10  23:27:52  lindner
  19.  * gopher 1.1 release
  20.  *
  21.  *
  22.  *********************************************************************/
  23.  
  24.  
  25. #include "GDgopherdir.h"
  26. #include "Malloc.h"
  27.  
  28.  
  29. #include <string.h>
  30. #include <stdio.h>
  31. extern int DEBUG;
  32.  
  33.  
  34. /***********************************************************************
  35. ** Stuff for GopherDirObjs
  36. **
  37. ***********************************************************************/
  38.  
  39.  
  40. GopherDirObj*
  41. GDnew(size)
  42.   int size;
  43. {
  44.      int i;
  45.      GopherDirObj *temp;
  46.      
  47.      temp = (GopherDirObj*) malloc(sizeof(GopherDirObj));
  48.  
  49.      temp->Gophers = DAnew(size, GSnew, GSinit, GSdestroy, GScpy);
  50.  
  51.      temp->Title = STRnew();
  52.      temp->currentitem = 1;
  53.  
  54.      GDinit(temp);
  55.      return(temp);
  56. }
  57.  
  58.  
  59. void
  60. GDdestroy(gd)
  61.   GopherDirObj *gd;
  62. {
  63.      int i;
  64.  
  65.      DAdestroy(gd->Gophers);
  66.      
  67.      STRdestroy(gd->Title);
  68.      free(gd);
  69. }
  70.  
  71.  
  72. void
  73. GDinit(gd)
  74.   GopherDirObj *gd;
  75. {
  76.      int i;
  77.  
  78.      DAinit(gd->Gophers);
  79.      STRinit(gd->Title);
  80. }
  81.  
  82.  
  83. extern int DEBUG;
  84.  
  85. /** This proc adds a GopherObj to a gopherdir. **/
  86. void
  87. GDaddGS(gd, gs)
  88.   GopherDirObj *gd;
  89.   GopherObj *gs;
  90. {
  91.     int top;
  92.  
  93.     DApush(gd->Gophers, gs);
  94. }
  95.  
  96.  
  97. /*
  98.  * Really weird!!!  We need this for qsort,  don't know why we can't use
  99.  * GScmp...
  100.  */
  101.  
  102. int
  103. GSqsortcmp(gs1, gs2)
  104.   GopherObj **gs1, **gs2;
  105. {
  106.      if (GSgetTitle(*gs1) == NULL)
  107.       return(1);
  108.      if (GSgetTitle(*gs2) == NULL)
  109.       return(-1);
  110.      
  111.      /** No numbering set on either entry, or both numbered
  112.          entries have the same number   **/
  113.  
  114.      if (GSgetNum(*gs1) == GSgetNum(*gs2))
  115.       return(strcmp(GSgetTitle(*gs1), GSgetTitle(*gs2)));
  116.  
  117.      /** first one numbered, second not **/
  118.      if (GSgetNum(*gs1) != -1 && GSgetNum(*gs2) == -1)
  119.       return(-1);
  120.  
  121.      /** second one numbered, first not **/
  122.      if (GSgetNum(*gs1) == -1 && GSgetNum(*gs2) != -1)
  123.       return(1);
  124.  
  125.      /** Both numbered, integer compare them **/
  126.  
  127.      return(GSgetNum(*gs1) - GSgetNum(*gs2));
  128. }
  129.  
  130. /*
  131.  * Sorts a gopher directory
  132.  */
  133.  
  134. void
  135. GDsort(gd)
  136.   GopherDirObj *gd;
  137. {
  138.  
  139.      DAsort(gd->Gophers, GSqsortcmp);
  140. }
  141.  
  142.  
  143. void
  144. GDtoNet(gd, sockfd)
  145.   GopherDirObj *gd;
  146.   int sockfd;
  147. {
  148.      int i;
  149.  
  150.      for (i=0; i< GDgetNumitems(gd); i++) {
  151.       GStoNet(GDgetEntry(gd, i), sockfd);
  152.      }      
  153.  
  154. }
  155.  
  156. void
  157. GDtoNetHTML(gd, sockfd)
  158.   GopherDirObj *gd;
  159.   int sockfd;
  160. {
  161.      int i;
  162.      
  163.      writestring(sockfd, "<MENU>\r\n");
  164.      
  165.      for (i=0; i< GDgetNumitems(gd); i++) {
  166.       writestring(sockfd, "<LI>");
  167.       GStoNetHTML(GDgetEntry(gd, i), sockfd);
  168.      }      
  169.      writestring(sockfd, "</MENU>");
  170. }
  171.  
  172.  
  173. /*
  174.  * Fill up a GopherDirObj with GopherObjs, given a gopher directory coming
  175.  * from sockfd.
  176.  *
  177.  * For each GopherObj retrieved, eachitem() is executed.
  178.  *
  179.  *  Returns 
  180.  
  181.  */
  182.  
  183. int
  184. GDfromNet(gd, sockfd, eachitem)
  185.   GopherDirObj *gd;
  186.   int sockfd;
  187.   int (*eachitem)();
  188. {
  189.      static GopherObj *TempGopher;
  190.      static char ZesTmp[1024];
  191.      int j, i;
  192.  
  193.      if (TempGopher == NULL)
  194.       TempGopher = GSnew();
  195.  
  196.      for (j=0; ; j++) {
  197.  
  198.       ZesTmp[0] = '\0';
  199.       
  200.       GSinit(TempGopher);
  201.       i = GSfromNet(TempGopher, sockfd);
  202.       
  203.       if (i==0) {
  204.            GDaddGS(gd, TempGopher);
  205.            if (eachitem != NULL) 
  206.             eachitem();
  207.       }
  208.  
  209.       else if (i==1)
  210.            return(j);
  211.  
  212.       /*** Unknown object type ***/
  213.       if (i== -2) {
  214.            j = j-1;
  215.            if (j<0) j=0;
  216.            readline(sockfd, ZesTmp, 1024); /** Get the rest of the line **/
  217.            ;
  218.       }
  219.       if (i==-1) {
  220.            j = j-1;
  221.            if (j<0) j=0;
  222.            readline(sockfd, ZesTmp, 1024); /** Get the rest of the line **/
  223.            return(j);
  224.       }
  225.      }
  226.  
  227.  
  228. /*
  229.  * Given an open file descriptor and an inited GopherDirobj,
  230.  *   read in gopher links, and add them to a gopherdir
  231.  */
  232.  
  233. void
  234. GDfromLink(gd, fd, host, port)
  235.   GopherDirObj *gd;
  236.   int          fd;
  237.   char         *host;
  238.   int          port;
  239. {
  240.      GopherObj *gs;
  241.  
  242.      gs = GSnew();
  243.  
  244.      while (GSfromLink(gs, fd, host, port) != -1) {
  245.       GDaddGS(gd, gs);
  246.       GSinit(gs);
  247.      }
  248.  
  249.      GSdestroy(gs);
  250. }
  251.  
  252.  
  253.  
  254. void
  255. GDtoLink(gd, fd)
  256.   GopherDirObj *gd;
  257.   int fd;
  258. {
  259.      int i;
  260.  
  261.      for (i=0; i< GDgetNumitems(gd); i++) {
  262.       GStoLink(GDgetEntry(gd, i), fd);
  263.      }      
  264.  
  265. }
  266.